<template>
  <div>
    <headers title="注册"></headers>
    <van-form @submit="onSubmit">
      <van-cell-group inset>
        <van-field
          v-model="username"
          name="username"
          label="用户名"
          placeholder="用户名"
          :rules="[
            { pattern: pattern1, required: true, message: '请填写正确用户名' },
          ]"
        />
        <van-field
          v-model="password"
          type="password"
          name="password"
          label="密码"
          placeholder="密码"
          :rules="[
            { pattern: pattern2, required: true, message: '请填写正确密码' },
          ]"
        >
          <template #button>
            <van-icon
              name="eye-o"
              size="30"
              v-if="isShow"
              @click="isShow = !isShow"
            />
            <van-icon
              name="closed-eye"
              size="30"
              v-else
              @click="isShow = !isShow"
            />
          </template>
        </van-field>
      </van-cell-group>
      <div style="margin: 16px">
        <van-button round block type="primary" native-type="submit">
          注册
        </van-button>
      </div>
    </van-form>
    <div style="width: 100%; text-align: right">
      <router-link to="/reg">立即登录</router-link>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import { loginApi } from "@/api/api";
import headers from "@/components/header.vue";
import { showToast } from "vant";


const router = useRouter();
const route = useRoute();
const isShow=ref(false)
const pattern1=/^1[3-9]\d{9}$/
const pattern2=/^\d{6}$/

 const username = ref('');
    const password = ref('');
    const onSubmit = (values) => {
      console.log('submit', values);
loginApi(username.value,password.value).then((res)=>{
  if(res.data.code==0){
    showToast("注册成功");

    router.go(-1);
  }else{
    showToast(res.data.msg);
  }
})
    };
</script>

<style lang="scss" scoped></style>
